<template>
    <div class="inventory-review-container">
    <!-- 搜索区域 -->
        <div class="search-section">
        <el-form :model="searchForm" :inline="true" class="search-form">
            <el-form-item label="盘点单号">
            <el-input v-model="searchForm.sheetNo" placeholder="请输入" clearable style="width: 180px" />
            </el-form-item>
            <el-form-item label="盘点类型">
            <el-select v-model="searchForm.type" placeholder="请选择" clearable style="width: 180px">
                <el-option label="存货盘点" value="存货盘点" />
            </el-select>
            </el-form-item>
            <el-form-item label="仓库名称">
            <el-select v-model="searchForm.warehouseName" placeholder="请选择" clearable style="width: 180px">
                <el-option label="盘点仓库一" value="盘点仓库一" />
            </el-select>
            </el-form-item>
            <el-form-item>
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button @click="handleReset">重置</el-button>
            </el-form-item>
        </el-form>
        </div>

            <!-- 操作按钮区域 -->
        <div class="action-section">
        <el-button type="success" size="default" @click="handleAdd">新增</el-button>

        </div>
        <!-- 表格区域 -->
        <div class="table-section">
        <el-table
            :data="tableData"
            border
            style="width: 100%"
            @selection-change="handleSelectionChange"
        >
            <el-table-column type="selection" width="55" />
            <el-table-column prop="reviewNo" label="盘点编号" min-width="120" />
            <el-table-column prop="warehouseName" label="仓库名称" min-width="120" />
            <el-table-column prop="reviewType" label="盘点类型" min-width="120" />
            <el-table-column label="盘点开始时间" min-width="120">
                <template #default="scope">
                    {{ formatDate(scope.row.startTime) }}
                </template>
            </el-table-column>
            <el-table-column label="盘点结束时间" min-width="120">
                <template #default="scope">
                    {{ formatDate(scope.row.endTime) }}
                </template>
            </el-table-column>
            <el-table-column prop="rstauts" label="状态" min-width="120" >
                <template #default="{row}">
                     {{ row.rstauts === 1 ? '启用' : '停用' }}
                </template>
            </el-table-column>

            <el-table-column prop="remark" label="备注" min-width="120" />
            <el-table-column label="操作" min-width="120" fixed="right">
            <template #default="scope">
                <el-button type="danger" text @click="handleRemove(scope.row)">删除</el-button>
            </template>
            </el-table-column>
        </el-table>
        </div>


    </div>
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import service from '@/utils/request'
import axios from 'axios'

const router = useRouter();

const tableData = ref([])
const selectedRows = ref<any[]>([])

// 格式化日期函数
const formatDate = (dateString: string) => {
  if (!dateString) return ''
  // 处理 ISO 格式的时间字符串，只显示年月日
  return dateString.split('T')[0]
}

const searchForm = reactive({
  sheetNo: '',
  result: '',
  type: '',
  warehouseName: ''
})

onMounted(() => {
    GetDate();
})

const GetDate=()=>{
    axios.get('http://59.110.150.130:8089/api/app/review-services/review', {
      params: {
        sheetNo: searchForm.sheetNo,
        result: searchForm.result,
        type: searchForm.type,
        warehouseName: searchForm.warehouseName
      }
    }).then(res => {
      tableData.value = res.data
    })
    .catch(err => {
        console.log(err)
    })
  }


const handleReset=()=>{
    searchForm.sheetNo = ''
    searchForm.result = ''
    searchForm.type = ''
    searchForm.warehouseName = ''
    GetDate();
}

function handleAdd()
{
    router.push('/AddReviewModel')
}

function handleRefresh(){
    GetDate()
    ElMessage.success('刷新成功')
}



function handleExport(){

}

function handleDelete(){
    if (selectedRows.value.length === 0) {
        ElMessage.warning('请选择要删除的盘点单')
        return
    }
    
    ElMessageBox.confirm(
        `确定要删除选中的 ${selectedRows.value.length} 个盘点单吗？`,
        '确认删除',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(() => {
        // 获取选中的盘点单号
        const sheetNos = selectedRows.value.map(row => row.reviewNo)
        
        // 调用删除API
        Promise.all(
            sheetNos.map(sheetNo => 
                service.post('/api/Inventory/DeleteInventory', { sheetNo })
            )
        ).then(() => {
            ElMessage.success('删除成功')
            GetDate() // 重新加载数据
        }).catch(error => {
            console.error('删除失败:', error)
            ElMessage.error('删除失败')
        })
    }).catch(() => {
        // 用户取消删除
        ElMessage.info('已取消删除')
    })
}


function handleSearch(){
    GetDate()
}

function handleSelectionChange(val: any) {
    selectedRows.value = val
}
function handleEdit(row: any) {
    router.push({
        path: '/AddReviewModel',
        query: { id: row.reviewNo }
    })
}
function handleRemove(row: any) {
    ElMessageBox.confirm(
        `确定要删除盘点单 "${row.reviewNo}" 吗？`,
        '确认删除',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(() => {
        // 调用删除API
        service.post('/api/Inventory/DeleteInventory', { sheetNo: row.reviewNo })
            .then(() => {
                ElMessage.success('删除成功')
                GetDate() // 重新加载数据
            })
            .catch(error => {
                console.error('删除失败:', error)
                ElMessage.error('删除失败')
            })
    }).catch(() => {
        // 用户取消删除
        ElMessage.info('已取消删除')
    })
}

</script>